package com.example.frank.test2;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Frank on 2017/12/27.
 */

public class LoginActivity extends Activity {

    private AutoCompleteTextView userid;
    private EditText password;
    private String str_userid, str_passwd,cookie;
    private SharedPreferences sp;
    private CheckBox auto_login,remember_passwd;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

//            userid.setText(setContentView(R.layout.activity_login));

            userid = findViewById(R.id.userid);
            password = findViewById(R.id.password);
            Button sign = findViewById(R.id.signin);
            remember_passwd = findViewById(R.id.check_passwd);
            auto_login = findViewById(R.id.auto_login);

            sp = getSharedPreferences("userInfo", 0);
            String name=sp.getString("USER_NAME", "");
            String pass =sp.getString("PASSWORD", "");

            boolean choseRemember =sp.getBoolean("remember", false);
            boolean choseAutoLogin =sp.getBoolean("autologin", false);

            if(choseRemember){
            userid.setText(name);
            password.setText(pass);
            remember_passwd.setChecked(true);
        }
        if(choseAutoLogin){
            auto_login.setChecked(true);
        }

        sign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                str_userid = userid.getText().toString();
                str_passwd = password.getText().toString();
                SharedPreferences.Editor editor =sp.edit();
                try {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                loginByHttpClientPost(str_userid, str_passwd);
                            } catch (IOException e) {
                                String err = e.toString();
                                Toast.makeText(LoginActivity.this, err, Toast.LENGTH_SHORT).show();
                            }
                        }
                    }).start();
                } catch (Exception e) {
                    Toast.makeText(LoginActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
                }
                editor.putString("USER_NAME", str_userid);
                editor.putString("PASSWORD", str_passwd);

                if(remember_passwd.isChecked()){
                    editor.putBoolean("remember", true);
                }else{
                    editor.putBoolean("remember", false);
                }

                if(auto_login.isChecked()){
                    editor.putBoolean("autologin", true);
                }else{
                    editor.putBoolean("autologin", false);
                }
                editor.apply();

            }
        });
    }
    //封装的登录类，包括登录状态
    public void loginByHttpClientPost(String userName, String Passwd) throws IOException {
        String path = "http://39.106.192.16:3000/user/logincheck";
        HttpPost httpPost = new HttpPost(path);
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        list.add(new BasicNameValuePair("username", userName));
        list.add(new BasicNameValuePair("password", Passwd));

        httpPost.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));
        String results = "";

        try {
            HttpResponse response = new DefaultHttpClient().execute(httpPost);
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                results = EntityUtils.toString(entity, HTTP.UTF_8);
                JSONObject jsonObject = new JSONObject(results);
                int status = jsonObject.optInt("status");
                System.out.println("返回的结果是"+results);
                switch (status) {
                    case 1:
                        cookie = jsonObject.optString("uid");
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        intent.putExtra("idcard", cookie);
                        startActivity(intent);
                        break;
                    case 0:
                        Looper.prepare();
                        Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                        Looper.loop();
                }
            } else {
            }
        } catch (Exception e) {
            results = e.toString();
            Looper.prepare();
            Toast.makeText(LoginActivity.this, results, Toast.LENGTH_SHORT).show();
            Looper.loop();

        }

    }

}


